package com.ruoyi.common.utils;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Response;

import java.io.IOException;

public class GDMapUtils {

    private static final String GD_KEY = "6122369a67f6b31dcc5eb079188e6d1a";

    public static String getAddressByCoordinate(double longitude, double latitude) {
        StringBuffer sb = new StringBuffer("https://restapi.amap.com/v3/geocode/regeo?output=json");
        sb.append("&location=").append(longitude).append(",").append(latitude);
        sb.append("&radius=").append("1000");
        sb.append("&extensions=").append("all");
        sb.append("&key=").append(GD_KEY);

        try {
            Response request = OkHttpUtils.getInstance().getRequest(sb.toString(), null);
            if (request.code() == 200) {
                String responseStr = request.body().string();
                JSONObject obj = JSON.parseObject(responseStr);
                if("1".equals(obj.getString("status"))){
                    return obj.getJSONObject("regeocode").getString("formatted_address");
                }
            }
            return "获取地理位置失败";
        } catch (IOException e) {
            return "获取地理位置失败";
        }
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 20; i++) {
//            System.out.println(getAddressByCoordinate(116.3798370,39.8655820));
//        }
//    }


}
